const LightManager = {
    roomLight: null,
    ambientLight: null,
    flashlight: null,
    isFlashlightOn: false,
    flashlightFlickerInterval: null,

    init(scene) {
        // 创建环境光（昏暗但可见）
        this.ambientLight = new THREE.AmbientLight(0x404040, 0.6);
        scene.add(this.ambientLight);

        // 创建点光源（灯）
        this.roomLight = new THREE.PointLight(0xffd700, 0, 8);
        this.roomLight.position.set(0, 3.5, 0);
        this.roomLight.castShadow = true;
        this.roomLight.shadow.mapSize.width = 1024;
        this.roomLight.shadow.mapSize.height = 1024;
        scene.add(this.roomLight);

        // 创建手电筒光源 - 与手电筒模型位置同步
        this.flashlight = new THREE.SpotLight(0xffffff, 0, 12, Math.PI / 8, 0.3);
        // 调整位置与手电筒模型一致
        this.flashlight.position.set(0.35, 1.4, -1.5);
        this.flashlight.target.position.set(0, 1.85, 0.1); // 指向坐着的人的头部
        this.flashlight.castShadow = true;
        this.flashlight.shadow.mapSize.width = 1024;
        this.flashlight.shadow.mapSize.height = 1024;
        this.flashlight.shadow.camera.near = 0.1;
        this.flashlight.shadow.camera.far = 15;
        this.flashlight.shadow.camera.fov = 30;
        scene.add(this.flashlight);
        scene.add(this.flashlight.target);

        // 添加一个方向光确保基础可见性
        const directionalLight = new THREE.DirectionalLight(0x404040, 0.4);
        directionalLight.position.set(5, 5, 5);
        scene.add(directionalLight);
    },

    toggleRoomLight(isOn) {
        if (isOn) {
            this.roomLight.intensity = 2.0;
            this.ambientLight.intensity = 0.5;
        } else {
            this.roomLight.intensity = 0;
            this.ambientLight.intensity = 0.6;
        }
    },

    startFlashlight() {
        this.isFlashlightOn = true;

        // 添加闪烁效果到UI
        const flashlightLabel = document.querySelector('label[for="flashlightSwitch"]');
        flashlightLabel.classList.add('flashlight-animation');

        // 高频闪烁效果
        this.flashlightFlickerInterval = setInterval(() => {
            if (this.isFlashlightOn) {
                // 在2.0和0之间快速切换强度，增强效果
                this.flashlight.intensity = this.flashlight.intensity > 0 ? 0 : 2.0;
            }
        }, 50); // 50ms间隔，比较高的频率
    },

    stopFlashlight() {
        this.isFlashlightOn = false;
        this.flashlight.intensity = 0;

        // 移除UI闪烁效果
        const flashlightLabel = document.querySelector('label[for="flashlightSwitch"]');
        flashlightLabel.classList.remove('flashlight-animation');

        // 清除闪烁间隔
        if (this.flashlightFlickerInterval) {
            clearInterval(this.flashlightFlickerInterval);
            this.flashlightFlickerInterval = null;
        }
    }
};